package cn.yunyichina.provider.finance.main;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.yunyichina.provider.finance.entity.AccountJournalPo;
import cn.yunyichina.provider.finance.service.impl.AccountJournalServiceImpl;
import cn.yunyichina.provider.framework.dubbo.service.impl.YytServiceImpl;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.finance.AccountJournalVo;
import cn.yunyichina.provider.iface.service.IFinance;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;

import java.util.List;

public class FinanceServiceImpl extends YytServiceImpl implements IFinance {
	
	private static final Logger logger = LoggerFactory.getLogger(FinanceServiceImpl.class);

	@Resource
	private AccountJournalServiceImpl accountJournalService;
	
	@Override
	public String getErrorCode() {
		return "22000";
	}
	
	/**
	 * 保存记账流水
	 * 
	 * @param orderNo
	 * @return
	 */
	@Override
	public ResCommon<Void> saveAccountJournal(String orderNo) {
		logger.info("保存记账流水，进入账务子系统 orderNo: " + orderNo);
		try {
			accountJournalService.saveAccountJournalByOrderId(orderNo);
		} catch (Exception e) {
			logger.error("保存记账流水异常", e);
			return new ResCommon<Void>("22001", e.getMessage());
		}
		
		return new ResCommon<Void>();
	}


	/**
	 * 修改记账流水
	 *
	 * @param accountJournalVo
	 * @return
	 */
	@Override
	public ResCommon<Void> updateAccountJournal(AccountJournalVo accountJournalVo) {
		logger.info("修改记账流水，进入账务子系统 orderNo: " + accountJournalVo.getOrderNo() +" , businessDesc:" + accountJournalVo.getBusinessDesc());
		try {
			accountJournalService.updateAccountJournal(JSON.toJSONString(accountJournalVo));
			logger.info("修改记账流水成功");
		} catch (Exception e) {
			logger.error("修改记账流水异常", e);
			return new ResCommon<Void>("22001", e.getMessage());
		}

		return new ResCommon<Void>();
	}

	/**
	 * 新增记账流水
	 *
	 * @param accountJournalVo
	 * @return
	 */
	@Override
	public ResCommon<Void> addAccountJournal(AccountJournalVo accountJournalVo) {

		logger.info("新增记账流水，进入账务子系统 orderNo: " + accountJournalVo.getOrderNo() +" , businessDesc:" + accountJournalVo.getBusinessDesc());
		try {
			accountJournalService.addAccountJournal(JSON.toJSONString(accountJournalVo));
			logger.info("新增记账流水成功");
		} catch (Exception e) {
			logger.error("保存记账流水异常", e);
			return new ResCommon<Void>("22001", e.getMessage());
		}

		return new ResCommon<Void>();
	}


	/**
	 * 查询记账流水
	 *
	 * @param data
	 * @return
	 */
	@Override
	public ResCommon<AccountJournalVo> selectAccountJournal(String data) {

		logger.info("查询记账流水，data: " + JsonUtils.toJsonString(data));

		ResCommon<AccountJournalVo> common = new ResCommon<>();
		try {
			Response response = accountJournalService.selectAccountJournal(JsonUtils.toJsonString(data));
			if("0".equals(response.getResultCode())){
				AccountJournalVo accountJournal = null;
				List<AccountJournalVo> journalPos = JsonUtils.parseArray(response.getResult(), AccountJournalVo.class);
				if(journalPos != null){
					for (AccountJournalVo journalPo : journalPos) {
						logger.info("journalPos orderNo:" + journalPo.getOrderNo() + " - userName:" + journalPo.getUserName() +" - Business_desc:" + journalPo.getBusinessDesc());
					}
					accountJournal = journalPos.get(0);
				}
				common.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE , Response.SUCCESS_MESSAGE , accountJournal);
			}else{
				logger.info("[FinanceServiceImpl.selectAccountJournal]  查无数据..");
				common.setResultCodeAndMessageAndResult(Response.FAILURE_CODE , "查无数据.." , null);
			}
		} catch (Exception e) {
			logger.error("保存记账流水异常", e);
			return new ResCommon<AccountJournalVo>("22001", e.getMessage());
		}

		return common;
	}
}
